/*
 * Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
 * All rights reserved
 */

#ifndef _BUTTON_H_
#define _BUTTON_H_

#include "string.h"
#include "stdint.h"
#include "usart3.h"
#include "font.h"
#include "at7456e.h"
#include "Task_OSD.h"
#include "usart6.h"
#include "Task_Timer.h"

//According to your need to modify the constants.
#define TICKS_INTERVAL    5	//ms
#define DEBOUNCE_TICKS    3	//MAX 7 (0 ~ 7)
#define SHORT_TICKS       (300 /TICKS_INTERVAL)
#define LONG_TICKS        (1000 /TICKS_INTERVAL)


#define	KEY_ON	0
#define	KEY_OFF	1
#define Null	0


typedef uint8_t (*BtnCallback)(void*);

typedef enum {
	PRESS_DOWN = 0,
	PRESS_UP,
	PRESS_REPEAT,
	SINGLE_CLICK,
	DOUBLE_CLICK,
	LONG_PRESS_START,
	LONG_PRESS_HOLD,
	number_of_event,
	NONE_PRESS
}PressEvent;

typedef struct Button {
	unsigned short ticks;
	unsigned char  repeat : 4;
	unsigned char  event : 4;
	unsigned char  state : 3;
	unsigned char  debounce_cnt : 3;
	unsigned char  active_level : 1;
	unsigned char  button_level : 1;
	unsigned char  button_id;
	unsigned char  (*hal_button_Level)(unsigned char button_id_);
	BtnCallback  cb[number_of_event];
	struct Button* next;
}Button;

typedef enum Button_IDs
{
    KEY_Menu_id = 0,
    KEY_Func_id,
    KEY_Up_id,
    KEY_Down_id,
	KEY_Laser_id,
	
	JOY_Up_id,
	JOY_Down_id,
	JOY_Left_id,
	JOY_Right_id,
	JOY_Center_id,
}_Button_IDs;
extern _Button_IDs Button_IDs;

typedef enum 
{
    Video_NV = 0,
    Video_IR,
}_Video_chanel;
extern _Video_chanel Video_chanel;

/**************模式定义********************/
typedef enum
{
	_Daylight,//白光
	_Infrared,//红外
}_OsdLight_Mode;
extern _OsdLight_Mode OsdLight_Mode; //OSD光线模式

typedef enum
{
	_PROFF = 0,		//停止
	_PhotoMode,//拍照
	_RecordMode,//录像
}_OsdRun_Mode;
extern _OsdRun_Mode OsdRun_Mode; //OSD运行模式

typedef enum
{
	_WIFI_OFF,	//关闭
	_WIFI_ON,		//开启
}_WIFI_Mode;
extern _WIFI_Mode WIFI_Mode; //Wifi运行模式

typedef enum
{
	_MotorStop,		//停止
	_Foreward,//正转
	_Reverse,//反转
}_MotorRun_Mode;
extern _MotorRun_Mode MotorRun_Mode; //电机运行模式

#if ChengDu
typedef enum
{
	_WhiteHot,//白热
	_BlackHot,//黑热
	_Rainbow,//彩红
	_Iron,//热金属
	_GreenHot,//绿热
}_Osd_Infrared_ColorMode;

#elif BT656

typedef enum
{
	_WhiteHot,//白热
	_BlackHot,//黑热
	_Iron,//热金属
	_GreenHot,//绿热
}_Osd_Infrared_ColorMode;

#endif

extern _Osd_Infrared_ColorMode Osd_Infrared_ColorMode; //OSD红外模式下的色彩模式

typedef enum
{
	_Auto,
	_Day,//彩色
	_Defog_Low,//透雾1
	_Defog_Mid,//透雾2
	_Defog_Hig,//透雾3
	_Night,//黑白
}_Osd_Daylight_ColorMode;
extern _Osd_Daylight_ColorMode Osd_Daylight_ColorMode; //OSD白光模式下的色彩模式

typedef enum
{
	_DWR_Off,
	_DWR_Low,
	_DWR_Mid,
	_DWR_Hig,
}_DWR_Pra;
extern _DWR_Pra DWR_Pra;

typedef enum
{
	_DNR_Off,
	_DNR_Low,
	_DNR_Mid,
	_DNR_Hig,
}_DNR_Pra;
extern _DNR_Pra DNR_Pra;

typedef enum
{
	_OutDoor_Off,
	_OutDoor_On,
	
}_OutDoor_Pra;
extern _OutDoor_Pra OutDoor_Pra;



/**************模式定义********************/


typedef enum
{
	_VL_AmplifierReduce_0,
	_VL_AmplifierReduce_1,
	_VL_AmplifierReduce_2,
	_VL_AmplifierReduce_3,
	_VL_AmplifierReduce_4,
	_VL_AmplifierReduce_5,
	_VL_AmplifierReduce_6,
	_VL_AmplifierReduce_7,
	_VL_AmplifierReduce_8,
	_VL_AmplifierReduce_9,
	_VL_AmplifierReduce_10,
	_VL_AmplifierReduce_11,
	_VL_AmplifierReduce_12,
	_VL_AmplifierReduce_13,
	_VL_AmplifierReduce_14,
	_VL_AmplifierReduce_15,
	_VL_AmplifierReduce_16,
		
}_VL_AmplifierReduce_Num;
extern _VL_AmplifierReduce_Num VL_AmplifierReduce_Num;


typedef enum
{
	_IR_AmplifierReduce_0,
	_IR_AmplifierReduce_1,
	_IR_AmplifierReduce_2,
	_IR_AmplifierReduce_3,
	_IR_AmplifierReduce_4,
	_IR_AmplifierReduce_5,
	_IR_AmplifierReduce_6,
	_IR_AmplifierReduce_7,
	_IR_AmplifierReduce_8,
	
}_IR_AmplifierReduce_Num;
extern _IR_AmplifierReduce_Num IR_AmplifierReduce_Num;

typedef enum
{
	_No_Stop,
	_Reduce_Stop,
	_Amplifier_Stop
}_MotorStop_Source;
extern _MotorStop_Source MotorRun_Source;

//菜单等级
typedef enum
{
	_MainMenu,
	_ConfigMenu,
	_ParameterSet
}_MenuLevel;
extern _MenuLevel MenuLevel;


///**********二级菜单，配置界面************/
//typedef enum
//{
//	_Brightness_SM,					//亮度
//	_ContrastRatio_SM,				//对比度
//	_Enhance_SM,							//增强
//	_Pip_SM,									//画中画
//	_FalseColor_SM,					//伪彩
//	_PR_SM,									//录像
//	_Wiry_SM								//Wify
//}_Pra_SM
//extern _Pra_SM	Pra_SM;


extern _Bool Flag_PhotoRecordeEnable;//拍照录像启动标识
extern bool Flag_LsserON;    //启光启动标识


//白光模式下的参数
typedef struct
{
	_OsdLight_Mode State;
	
}_DaylightParameter;

//红外模式下的参数
typedef struct
{
	
}_InfraredParameter;

typedef enum 
{
    Auto = 0,
    Color,
    Mono,
}_NV_Model;
extern _NV_Model NV_Model;

extern struct Button KEY_Menu;
extern struct Button KEY_Func;
extern struct Button KEY_Up;
extern struct Button KEY_Down;
extern struct Button KEY_Laser;
extern _Bool Video_stat;

// 红外机芯控制命令定义
extern const unsigned char Enter_click[];
extern const unsigned char Enter_long[];
extern const unsigned char Up_click[];
extern const unsigned char Up_long[];
extern const unsigned char Down_click[];
extern const unsigned char Down_long[];
extern const unsigned char Left_click[];
extern const unsigned char Left_long[];
extern const unsigned char Right_click[];
extern const unsigned char Right_long[];
extern const unsigned char Power_click[];

// 白光模式命令
extern const unsigned char NV_Prefix[];
extern const unsigned char NV_Suffix[];
extern const unsigned char NV_X_Key;
extern const unsigned char NV_Model_Key;

// WiFi命令
extern unsigned char WiFi_OFF[];
extern unsigned char WiFi_ON[];
extern unsigned char Capture_OFF[];
extern unsigned char Capture_ON[];
extern unsigned char Take_Photo[];

extern bool Flag_ButtonWaiting;


// Menu 按键回调函数
uint8_t KEY_MENU_SINGLE_Click_Handler(void *btn);
uint8_t KEY_MENU_DOUBLE_Click_Handler(void *btn);
uint8_t KEY_MENU_LONG_PRESS_START_Handler(void *btn);
uint8_t KEY_MENU_LONG_PRESS_HOLD_Handler(void *btn);

// Func 按键回调函数
uint8_t KEY_FUNC_SINGLE_Click_Handler(void *btn);
uint8_t KEY_FUNC_DOUBLE_Click_Handler(void *btn);
uint8_t KEY_FUNC_LONG_PRESS_START_Handler(void *btn);
uint8_t KEY_FUNC_LONG_PRESS_HOLD_Handler(void *btn);

// Up 按键回调函数
uint8_t KEY_UP_SINGLE_Click_Handler(void *btn);
uint8_t KEY_UP_DOUBLE_Click_Handler(void *btn);
uint8_t KEY_UP_LONG_PRESS_START_Handler(void *btn);
uint8_t KEY_UP_LONG_PRESS_HOLD_Handler(void *btn);

// Down 按键回调函数
uint8_t KEY_DOWN_SINGLE_Click_Handler(void *btn);
uint8_t KEY_DOWN_DOUBLE_Click_Handler(void *btn);
uint8_t KEY_DOWN_LONG_PRESS_START_Handler(void *btn);
uint8_t KEY_DOWN_LONG_PRESS_HOLD_Handler(void *btn);

//激光测距
uint8_t KEY_Laser_SINGLE_Click_Handler(void *btn);
uint8_t KEY_Laser_DOUBLE_Click_Handler(void *btn);
uint8_t KEY_Laser_LONG_PRESS_START_Handler(void *btn);
uint8_t KEY_Laser_LONG_PRESS_HOLD_Handler(void *btn);
uint8_t KEY_Laser_PRESS_UP(void *btn);

//JOY_Up
uint8_t JOY_Up_SINGLE_Click_Handler(void *btn);
uint8_t JOY_Up_DOUBLE_Click_Handler(void *btn);
uint8_t JOY_Up_LONG_PRESS_START_Handler(void *btn);
uint8_t JOY_Up_LONG_PRESS_HOLD_Handler(void *btn);
uint8_t JOY_Up_PRESS_UP(void *btn);

//JOY_Down
uint8_t JOY_Down_SINGLE_Click_Handler(void *btn);
uint8_t JOY_Down_DOUBLE_Click_Handler(void *btn);
uint8_t JOY_Down_LONG_PRESS_START_Handler(void *btn);
uint8_t JOY_Down_LONG_PRESS_HOLD_Handler(void *btn);
uint8_t JOY_Down_PRESS_UP(void *btn);

//JOY_Left
uint8_t JOY_Left_SINGLE_Click_Handler(void *btn);
uint8_t JOY_Left_DOUBLE_Click_Handler(void *btn);
uint8_t JOY_Left_LONG_PRESS_START_Handler(void *btn);
uint8_t JOY_Left_LONG_PRESS_HOLD_Handler(void *btn);
//button_attach(&KEY_Up, PRESS_UP, KEY_UP_PRESS_UP);

//JOY_Right
uint8_t JOY_Right_SINGLE_Click_Handler(void *btn);
uint8_t JOY_Right_DOUBLE_Click_Handler(void *btn);
uint8_t JOY_Right_LONG_PRESS_START_Handler(void *btn);
uint8_t JOY_Right_LONG_PRESS_HOLD_Handler(void *btn);
//button_attach(&KEY_Down, PRESS_UP, KEY_DOWN_PRESS_UP);

//JOY_Center
uint8_t JOY_Center_SINGLE_Click_Handler(void *btn);
uint8_t JOY_Center_DOUBLE_Click_Handler(void *btn);
uint8_t JOY_Center_LONG_PRESS_START_Handler(void *btn);
uint8_t JOY_Center_LONG_PRESS_HOLD_Handler(void *btn);

void ButtonInit(void);

// 按键读取函数
unsigned char read_key_level(unsigned char button_id);

#ifdef __cplusplus
extern "C" {
#endif

void button_init(struct Button* handle, unsigned char(*pin_level)(unsigned char), unsigned char active_level, unsigned char button_id);
void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
PressEvent get_button_event(struct Button* handle);
int  button_start(struct Button* handle);
void button_stop(struct Button* handle);
void button_ticks(void);
uint8_t KEY_UP_PRESS_UP(void *btn);
uint8_t KEY_DOWN_PRESS_UP(void *btn);


extern void Set_OsdLight_Mode(_OsdLight_Mode  Mode);
extern void Set_Osd_PhotoRecord(_OsdRun_Mode Mode);
extern void Set_Osd_Infrared_ColorMode(_Osd_Infrared_ColorMode Mode);
extern void Set_Osd_Daylight_ColorMode(_Osd_Daylight_ColorMode Mode);
extern void Set_Osd_LightMode(_OsdLight_Mode Mode);
extern void RecordIconBlink(_OsdRun_Mode OsdRun_Mode);
extern uint8_t Control_Num_Blink(_MenuLevel MenuLevel, _ConfigMenuPra *ConfigMenuPra);

extern void MotorForwardReverse_OsdShow(_MotorRun_Mode Mode);
//使拍照图标闪烁
extern void PhotoIconBlink(_OsdRun_Mode OsdRun_Mode);
//光标下移
void CursorDown(_ConfigMenuPra *ConfigMenuPra);
//光标上移
void CursorUp(_ConfigMenuPra *ConfigMenuPra);
//光标右移
void CursorRight(_ConfigMenuPra *ConfigMenuPra);
void CursorLeft(_ConfigMenuPra *ConfigMenuPra);
void Set_Osd_Wifi(_WIFI_Mode Mode);
//设置白光模式下的动态范围
void Set_Osd_Daylight_DWDR(_DWR_Pra DWDR);
//设置白光模式下的颜色模式
void Set_Osd_Daylight_DNR(_DNR_Pra DNR);

void Set_Osd_Daylight_ColorMode(_Osd_Daylight_ColorMode Daylight_ColorMode);

#ifdef __cplusplus
}
#endif

#endif
